<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="decorator" uri="http://claudiushauptmann.com/jsp-decorator/"%>
<%@ page import="vn.fpt.fsoft.entity.User" %>

<decorator:decorate filename="MasterPage.jsp">
    <decorator:content placeholder="title">List Trust Region</decorator:content>
    <decorator:content placeholder="head">
        <link rel="stylesheet" href="css/jq.css" type="text/css"
            media="print, projection, screen" />
        <link rel="stylesheet" href="css/style-tablesort.css" type="text/css"
            media="print, projection, screen" />
        <script
            src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
        </script>
        <script type="text/javascript" src="js/jquery.tablesorter.js"></script>
        <script type="text/javascript" src="js/jquery.tablesorter.pager.js"></script>
        <script type="text/javascript" src="js/docs.js"></script>

        <script type="text/javascript"
            src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="http://bootboxjs.com/bootbox.js"></script>
        <link href="http://getbootstrap.com/dist/css/bootstrap.min.css"
            rel="stylesheet" type="text/css" />
        <link rel="stylesheet" href="css/style-homepage.css" type="text/css">
        <link rel="stylesheet"
            href="http://www.script-tutorials.com/demos/87/css/style.css"
            type="text/css" media="screen">
        <script type="text/javascript">
        function amendTrustRegion(regionID) {
        //  alert("fdsf");      
            bootbox.dialog({
                message : "Do you want to amend this Trust Trust Region?",
                buttons : {
                    upadate : {
                        label : "Amend",
                        className : "btn-upadte",
                        callback : function() {
                            window.location
                            .replace("AmendTrustRegion.jsp?regionID="+ regionID);
                        }
                    },
                    cancel : {
                        label : "Cancel",
                        className : "btn-cancel",
                    }
                }
            });
        }
            function Click_Record(trustRegionID,isActive) {
                if (!isActive) {
                    bootbox.confirm("Do you want to make this Trust Region active?", 
                            function(result) {
                                if(result) {
                                var check = $("#checkActive").prop("checked");
                                $.ajax({
                              url:"TrustRegionController?action=active&trustregionid="+trustRegionID 
                                      + "&checked=" + check ,
                              type: "POST",
                              success:function(data) {
                                 load(data);
                              },
                              error:function(data){
                        
                            }
                         });
                    }
                })
            } else {
            	amendTrustRegion(trustRegionID);
            }
        }
        function Click_Create() {
            window.location.replace("AddTrustRegion.jsp");
        }
        function load(data) {
            var data1 = JSON.parse(data);
            var length = data1.TrustRegion.length;
            var result = "<table cellspacing=1 class=tablesorter id=table>";
            result += "<thead>";
            result += "<tr>";
            result += "<th>Trust Region Name</th>";
            result += "<th>Description</th>";
            result += "<th>Nation/Country</th>";
            result += "<th>Is Active?</th>";
            result += "</tr>";
            result += "</thead>";
            result += "<tbody>";
            $("#datatable").empty();
            for(var i = 0; i < length; i++) {
                result += "<tr onclick=Click_Record("+data1.TrustRegion[i].TrustRegionID+","+data1.TrustRegion[i].IsActive+");>";
                result += "<td>" + data1.TrustRegion[i].Name + "</td>";
                result += "<td>" + data1.TrustRegion[i].Descript + "</td>";
                result += "<td>" + data1.TrustRegion[i].CountryName + "</td>";
                if(data1.TrustRegion[i].IsActive) {
                    result += "<td>Yes</td>";
                } else {
                    result += "<td>No</td>";
                }
                result += "</tr>";
            }
            result += "</tbody>";
            result += "</table>";
            result += "<div id=pager class=pager style=right:5px;float:right;>";
            result += "<form>";
            result += "<img src=img/first.png class=first />";
            result += "<img src=img/prev.png class=prev />";
            result += " <input type=text class=pagedisplay size=5 style=\"width:63px; height:20px;\"/>";
            result += "<img src=img/next.png class=next />";
            result += "<img src=img/last.png class=last />";
            result += "<select class=pagesize style=visibility:hidden;>";
            result += "<option selected=selected  value=15></option>";
            result += "</select>";
            result += "</form>";
            result += "</div>";
            $("#datatable").append(result);
            $("#table")
            .tablesorter({widthFixed: true, widgets: ['zebra']})
            .tablesorterPager({container: $("#pager")});
            
            $("#pager").css("position","static");
        } 
        $(document).ready(function() {
            $("a").click(function(){
                 var idclick = $(this).attr("id");
                 var check = $("#checkActive").prop("checked");
                 $.ajax({
                      url:"TrustRegionController?action=listTrustRegion&idclick=" + idclick + "&checked=" + check,
                      type: "POST",
                      success:function(data) {
                           load(data);
                       },
                       error:function(data){
                            
                      }
                   });
               })
            $("#checkActive").click(function() {
                var check = $("#checkActive").prop("checked");
                $.ajax({
                       url:"TrustRegionController?action=listTrustRegion&checked="+check,
                       type: "POST",
                       success:function(data) {
                            load(data);
                       },
                       error:function(data){
                                
                       }
                   });
                }); 
            });
            $(window).load(function(){  
                $.ajax({
                    url:"TrustRegionController?action=listTrustRegion&checked=false",
                    type: "POST",
                    success:function(data) {
                        load(data);
                    },
                    error:function(data){
                        alert("fail");
                    }
                    });
            })
         </script>  
    </decorator:content>
    <decorator:content placeholder="body">
    <%
		User user = (User) request.getSession().getAttribute("user");
		if(user == null) {
		    response.sendRedirect("login.jsp");
		}
	%>
        <p style="color: #0C3; font-size: 24px; font-weight: bold;">Trust Region List</p>
        <div id="manager_nav">
            <ul>
                <li><a href="#" id="all">All</a></li>
                <li>|</li>
                <li><a href="#" id="digital">0 - 9</a></li>
                <li>|</li>
                <li><a href="#" id="ae">A B C D E</a></li>
                <li>|</li>
                <li><a href="#" id="fj">F G H I J</a></li>
                <li>|</li>
                <li><a href="#" id="kn">K L M N</a></li>
                <li>|</li>
                <li><a href="#" id="or">O P Q R</a></li>
                <li>|</li>
                <li><a href="#" id="sv">S T U V</a></li>
                <li>|</li>
                <li><a href="#" id="wz">W X Y Z</a></li>
            </ul>
    </div>
    <div id="manager_action">
        <a href="#" style="text-decoration: none">
            <input type="button" value="Create" onclick="Click_Create();"/></a>&nbsp; 
            <input type="checkbox" id="checkActive">
            <span style="color: gray; font-weight: bold">Include in-active</span>
    </div>
    <br>
    <div id="datatable" ></div>
        
    </decorator:content>
</decorator:decorate>